package com.childmiles.gwt.client.ui;

import java.util.List;

import com.childmiles.gwt.client.domain.Award;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;

public class AwardPanel extends Composite {
    FlowPanel contents;
    private final Integer dayNumber;
    private List<Award> awards;
    
    public AwardPanel (Integer dayNumber) {
        this.dayNumber = dayNumber;
        contents = new FlowPanel();
        contents.setTitle("Click to edit");
        contents.addStyleName("awardPanel");
        contents.add(new Label(dayNumber.toString()));
        initWidget(contents);
    }

    public AwardPanel(Integer dayNumber, List<Award> awards) {
        this(dayNumber);
        //Can't let awards be null here
        this.awards = awards;
        refreshContents(awards);
    }
    
//    public void addAward(Award award) {
//        awards.add(award);
//        refreshContents();
//    }
//    
    /**
     * @param award
     */
    public void removeAward(Award award) {
        for (int i=0; i< awards.size(); i++) {
            Award existingAward = awards.get(i);
            if (existingAward.getAdded().equals(award.getAdded())) {
                awards.remove(i);
                break;
            }
        } 
        refreshContents(awards);
    }
    
    public void refreshContents(List<Award> awards) {
        this.awards=awards;
        contents.clear();
        contents.add(new Label("Day " + dayNumber));
        for (Award award : awards) {
//            contents.add(new Label(award.getName()));
            Image awardImage = new Image(award.getImageURL());
            awardImage.addStyleName("awardImage");
            contents.add(awardImage);   
        }
    }

    public List<Award> getAwards() {
        return awards;
    }

    public void setAwards(List<Award> awards) {
        this.awards = awards;
    }

    public Integer getDayNumber() {
        return dayNumber;
    }
}
